<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日期计算</title>

    <script>
      function cdate() {
        var startDate = new Date(document.getElementById("startDate").value);
        var endDate = new Date(document.getElementById("endDate").value);
        var timeDifference = endDate - startDate;
        var daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
        if (isNaN(startDate) || isNaN(endDate)) {
                var r1 = document.getElementById("result");
                 r1.innerHTML = "请输入有效日期！！！！！！！！！！！";
            } 
            else if(isNaN(startDate) & isNaN(endDate)){
                var r1 = document.getElementById("result");
                r1.innerHTML = "请输入有效日期！！！！！！！！！！！";
            }
            else if(daysDifference<0){
                var r1 = document.getElementById("result");
                r1.innerHTML = "请输入有效日期！！！！！！！！！！！";
            }
            else {
                 
                 document.getElementById("result").innerHTML = "两个日期相差 " + daysDifference + " 天";
            }
                }
    </script>
</head>
<body>
    <h1>日期差计算器</h1>
    <!-- <label for="startDate">开始日期：</label> -->
     <h1>开始日期：</h1>
    <input type="date" id="startDate">
    <br>
    <!-- <label for="endDate">结束日期：</label> -->
     <h1>结束日期：</h1>
    <input type="date" id="endDate">
    <br>
    <button onclick="cdate()">计算天数差</button>
    <p id="result"></p>
</body>
</html>




 
 
